package com.example.building_material_mannager.service.impl;

import com.example.building_material_mannager.entity.*;
import com.example.building_material_mannager.mapper.CollectionFlowMapper;
import com.example.building_material_mannager.mapper.CollectionlsMapper;
import com.example.building_material_mannager.mapper.SellMapper;
import com.example.building_material_mannager.service.CollectionlsService;
import com.example.building_material_mannager.util.PageUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
@Service
public class CollectionlsServiceImpl implements CollectionlsService {
    @Resource
    CollectionlsMapper collectionMapper;
    @Resource
    SellMapper sellMapper;
    @Resource
    CollectionFlowMapper collectionFlowMapper;

    //显示分页条件查询
    @Override
    public PageUtil<List<CollectionlsAndSell>> selectCollectionls(SelectType collectionSelectType) {
        //查询应付
        List<Collectionls> collections = collectionMapper.selectCollectionls(collectionSelectType.getClosingStatus());
        //创建分页对象
        PageUtil<List<CollectionlsAndSell>> pageBean = new PageUtil<>();
        //收款流水对象
        List<CollectionFlow> collectionFlows;
        //创建销售订单和应付关联对象集合
        List<CollectionlsAndSell> collectionlsAndSells = new ArrayList<>();
        //创建关联对象
        CollectionlsAndSell collectionlsAndSell = new CollectionlsAndSell(); //根据每条应付查询销售订单
        for (Collectionls collectionl : collections) {
            //查询相应销售订单
            Sell sells = sellMapper.selectByOrderByCollectionls(collectionl.getCosordernumber(), collectionSelectType);
            //判断销售相应订单是否存在
            if (sells != null) {
                //非空时，即设置了收款日期查询
                if (collectionSelectType.getPaymentDateStart() != null) {
                    //查询
                    collectionFlows = collectionFlowMapper.selectCollectionByCfcoid(collectionl.getCoid(), collectionSelectType.getPaymentDateStart(), collectionSelectType.getPaymentDateEnd());
                    if (collectionFlows.size()<1) {
                        continue;
                    }
                }
                //为空时，收款日期条件未启用
                else {
                    collectionFlows = collectionFlowMapper.selectCollectionByCfcoid(collectionl.getCoid(), collectionSelectType.getPaymentDateStart(), collectionSelectType.getPaymentDateEnd());

                }
                //收款流水数据正常注入
                collectionlsAndSell.setCollectionFlows(collectionFlows);
                //销售数据注入
                collectionlsAndSell.setSell(sells);
                //应收数据注入
                collectionlsAndSell.setCollectionls(collectionl);
                //添加入关联集合
                collectionlsAndSells.add(collectionlsAndSell);
                //System.out.println(collectionlsAndSell);
            }
        }
        //分页内容
        pageBean.setData(collectionlsAndSells);
        return pageBean;
    }
}
